using System.Text.RegularExpressions;
using System.IO;
using System.Linq;
using Orchard.UI.Resources;
using System.Web;
using Orchard;

namespace Line20.DateTimeRangeField {
    public class ResourceManifest : IResourceManifestProvider {
        public ResourceManifest(IWorkContextAccessor workContextAccessor) {
            if (workContextAccessor != null && workContextAccessor.GetContext() != null) {
                var siteCulture = workContextAccessor.GetContext().CurrentCulture;
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(siteCulture);
            }
        }

        public void BuildManifests(ResourceManifestBuilder builder) {
            var manifest = builder.Add();

            // Front End
            manifest.DefineStyle("Line20.DateTimeRangeField.FrontEnd.Style").SetUrl("datetimerange.css");

            // Back End
            manifest.DefineStyle("Line20.DateTimeRangeField.BackEnd.Style").SetUrl("datetime.css").SetDependencies("jQueryUI_DatePicker", "jQueryUI_Orchard");
            manifest.DefineScript("Line20.DateTimeRangeField.DateTimePicker").SetUrl("jquery-ui-timepicker-addon.js").SetDependencies("jQuery", "jQueryUtils", "jQueryUI_Core", "jQueryUI_Widget", "jQueryUI_Mouse", "jQueryUI_Button", "jQueryUI_Slider", "jQueryUI_DatePicker");
            manifest.DefineScript("Line20.DateTimeRangeField.Automation").SetUrl("date-time-range-automation.js").SetDependencies("Line20.DateTimeRangeField.DateTimePicker");
            manifest.DefineScript("Line20.DateTimeRangeField.Hints").SetUrl("hint.js");
            manifest.DefineScript("Line20.DateTimeRangeField.Localize.TimePicker").SetBasePath(manifest.BasePath + "scripts/localization/").SetUrl("jquery.ui.timepicker.js").SetCultures(SupportedTimePickerCultures(manifest)).SetDependencies("Line20.DateTimeRangeField.DateTimePicker");
            manifest.DefineScript("Line20.DateTimeRangeField.Localize.DatePicker").SetBasePath(manifest.BasePath + "scripts/localization/").SetUrl("jquery.ui.datepicker.js").SetCultures(SupportedDatePickerCultures(manifest)).SetDependencies("Line20.DateTimeRangeField.DateTimePicker");
        }

        public string[] SupportedDatePickerCultures(IResourceManifest manifest) {
            return GetCulturesFromFiles(manifest, "jquery.ui.datepicker");
        }

        public string[] SupportedTimePickerCultures(IResourceManifest manifest) {
            return GetCulturesFromFiles(manifest, "jquery.ui.timepicker");
        }

        private string[] GetCulturesFromFiles(IResourceManifest manifest, string searchPattern) {
            Regex regex = new Regex(searchPattern + @".([\w|-]+).js");
            var supportedDatePickerCultures = (from file in GetFiles(manifest, searchPattern + "*.*")
                                               let match = regex.Match(file)
                                               where match.Groups.Count == 2     // first group is the entire match, second group is the culture
                                               select regex.Match(file).Groups[1].Value).ToArray();

            return supportedDatePickerCultures;
        }


        private string[] GetFiles(IResourceManifest manifest, string searchPattern) {
            string path = HttpContext.Current.Server.MapPath(manifest.BasePath + "scripts/localization/");
            return Directory.GetFiles(path, searchPattern);
        }
    }
}
